﻿using EnvCheck.Models;
using EnvCheck.Utils;
using EnvCheck.Services;
using EnvCheck.View.Export;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using EnvCheck.View;
namespace EnvCheck.View.Export;
public class CurrentEnvViewModel : BaseViewModel
{
    public CurrentEnv? envView{get;}
    public CurrentEnvViewModel(CurrentEnv? env=null)
    {
        envView = env;
    }
    private int _loadProgress;
    public int LoadProgress
    {
        get => _loadProgress;
        private set
        {
            if (_loadProgress != value)
            {
                _loadProgress = value;
                OnPropertyChanged(nameof(LoadProgress));
                OnPropertyChanged(nameof(ProgressText));
                OnPropertyChanged(nameof(ProgressValue));
                OnPropertyChanged(nameof(IsSubmittable));
                OnPropertyChanged(nameof(IsLoading));
            }
        }
    }

    private int _total;
    public int Total
    {
        get => _total;
        private set
        {
            if (_total != value)
            {
                _total = value;
                OnPropertyChanged(nameof(Total));
                OnPropertyChanged(nameof(ProgressText));
                OnPropertyChanged(nameof(ProgressValue));
                OnPropertyChanged(nameof(IsSubmittable));
                OnPropertyChanged(nameof(IsLoading));
            }
        }
    }

    public bool IsSubmittable => LoadProgress >= Total && Total > 0 && NewVersionName.Length>0;

    // 加载中的反义，方便绑定按钮的 IsEnabled
    public bool IsLoading => !IsSubmittable;

    public string ProgressText
        => Total == 0 ? "0%" : $"{(100.0 * LoadProgress / Total):F1}% ({LoadProgress}/{Total})";

    // ProgressBar 的值要求 0~1 之间的小数
    public double ProgressValue => Total == 0 ? 0 : 1.0*LoadProgress / Total;

    public string SaveButtonPrompt { get; set; } = "正在加载...";

    private string _newVersionName = "";
    public string NewVersionName
    {
        get => _newVersionName;
        set
        {
            if (_newVersionName != value)
            {
                _newVersionName = value;
            }
            OnPropertyChanged(nameof(NewVersionName));
            OnPropertyChanged(nameof(IsSubmittable));
        }
    }

    public ObservableCollection<AppVersionRequirement> InstalledApps { get; } = new();

    public CurrentEnvViewModel()
    {
        LoadInstalledAppsAsync();
    }

    private async void LoadInstalledAppsAsync()
    {
        LoadProgress = 0;
        Total = 0;


        await Task.Run(() =>
        {
            InstalledAppService.AsyncLoadInstalledApps(this, this);
            
            SaveButtonPrompt = "保存当前环境";
            OnPropertyChanged(nameof(NewVersionName));
            OnPropertyChanged(nameof(SaveButtonPrompt));
        });

        // 确保最终状态更新
        Application.Current.Dispatcher.Invoke(() =>
        {
            LoadProgress = Total;
        });
    }

    public override void Receive(string name, string version)
    {
        Application.Current.Dispatcher.Invoke(() =>
        {
            InstalledApps.Add(new AppVersionRequirement { Name = name, ExpectedVersion = version });
            OnPropertyChanged(nameof(ProgressText));
            OnPropertyChanged(nameof(ProgressValue));
        });
    }

    public override void ReportProgress(int current, int total)
    {
        Application.Current.Dispatcher.Invoke(() =>
        {
            LoadProgress = current;
            Total = total;
            OnPropertyChanged(nameof(ProgressText));
            OnPropertyChanged(nameof(ProgressValue));
        });
    }


}
